/*
 * B00188409    B00168419   B00210129
 */

package main;

import java.util.Random;
import java.io.IOException;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.Image;
import java.io.InputStream;

public class GameManager extends LayerManager {

    private Image bangImage;
    private static final String BANG_IMAGE    = "/resource/JinAllSheet.png";
    private static final int    BANG_WIDTH    = 49;
    private static final int    BANG_HEIGHT   = 50;

    private Image nailImage ;
    private static final String NAIL_IMAGE    = "/resource/Jinsword.png";
    private static final int    NAIL_WIDTH    = 66;
    private static final int    NAIL_HEIGHT   = 24;

    private Image jinImage;
    private static final String JIN_IMAGE    = "/resource/BangAllSheet.png";
    private static final int    JIN_WIDTH    = 49;
    private static final int    JIN_HEIGHT   = 75;

    private Image shardImage ;
    private static final String SHARD_IMAGE    = "/resource/Nail.png";
    private static final int    SHARD_WIDTH    = 41;
    private static final int    SHARD_HEIGHT   = 10;

    private int canvasX, canvasY;
    private int leftPosX=0, leftPosY=0;// game windows
    private int height, width;

    protected   boolean GameOver    = false; // game condition
    private     int     score       = 0; // score
    private     Random  r           = new Random(System.currentTimeMillis()); // random generator
    protected   BBCanvas    gameCanvas;

    private     Bang       bang;
    private     Jin             jin;
    private     Nail          nail ;
    private     Shard         shard;

    private Nail newNail;
    private Shard newShard;

    public GameManager(int x, int y, int height, int width, BBCanvas theCanvas) throws IOException{
        super();
        this.canvasX = x;
        this.canvasY = y;
        this.height = height;
        this.width = width;
        this.gameCanvas = theCanvas;
        setViewWindow(leftPosX,leftPosY,width,height);

        bangImage = Image.createImage(BANG_IMAGE);
 
        bang = new Bang(bangImage, BANG_WIDTH, BANG_HEIGHT);
       
        jinImage = Image.createImage(JIN_IMAGE);

        jin = new Jin(jinImage, JIN_WIDTH, JIN_HEIGHT);

        jin.setRefPixelPosition(height/2, width/2);
        this.append(jin);
        this.append(bang);

        nailImage = Image.createImage(NAIL_IMAGE);
        nail = new Nail (nailImage, NAIL_WIDTH, NAIL_HEIGHT);
        bang.initNail(nailImage, NAIL_WIDTH, NAIL_HEIGHT);

        shardImage = Image.createImage(SHARD_IMAGE);
        shard = new Shard (shardImage, SHARD_WIDTH, SHARD_HEIGHT);
        jin.initShard(shardImage, SHARD_WIDTH, SHARD_HEIGHT);
        
    }
    
    public void paint(Graphics g) {

        paint(g,canvasX,canvasY);

        g.setColor(255, 0, 0);
        g.fillRect(canvasX, canvasY, width/2, 10);

        g.setColor(125, 255, 10);
        if (bang.getHPPercentage()>=0)
            g.fillRect(canvasX+1, canvasY+1, convert2Long((bang.getHPPercentage())*(width/2)), 9);

        g.setColor(255, 255, 255);
        g.drawString("" + score, width - 20, 10, g.TOP|g.LEFT);

        if (GameOver) endGame(g);
    }

    public void advance(int ticks) {

        int keyState = gameCanvas.getKeyStates();

        if ((keyState & GameCanvas.RIGHT_PRESSED) != 0){
            jin.moveRight(width);
        }

        if ((keyState & GameCanvas.LEFT_PRESSED) != 0){
            jin.moveLeft();
        }
 
        if ((keyState & GameCanvas.UP_PRESSED) != 0){
            jin.moveUp();
            bang.moveUp();
        }

        if ((keyState & GameCanvas.DOWN_PRESSED) != 0){
            jin.moveDown(height);
            bang.moveDown(height);
        }
        
        if ((keyState & GameCanvas.FIRE_PRESSED) != 0){
           try {
              newNail = bang.fire(ticks);
              newShard = jin.fire(ticks);

              if (newNail != null)  {
                  this.remove(nail);
                  nail = newNail;
                  this.append(nail);
              }

              if (newShard != null)  {
                  this.remove(shard);
                  shard = newShard;
                  this.append(shard);
              }
           }

       catch (java.lang.ArrayIndexOutOfBoundsException e) {e.printStackTrace();}

        if (nail!= null)
            nail.advance(ticks);
        if (!nail.isAppear())
            this.remove(nail);

        if (shard!= null)
            shard.advance(ticks);
        if (!shard.isAppear())
            this.remove(shard);
            }

        bang.advance(ticks);
        jin.advance(ticks);
        shard.advance(ticks);
        nail.advance(ticks);

        if(nail.collidesWith(jin, false)){
            bang.collided(ticks, 5);

        }

        if(shard.collidesWith(bang, true))
            score += 50;
        }

    public int getRandom ( int min, int max) {
        return (Math.abs(r.nextInt()))%(max-min) + min;
    }

   
    protected void endGame(Graphics g) {
        GameOver=true;
        gameCanvas.stop();
        Font f = g.getFont();
        int h = f.getHeight()+40;
        int w = f.stringWidth("High score")+40;
        g.setColor(250,250,250);
        g.drawString("Score " + score,(width-w)/2,(height-h)/2,g.TOP | g.LEFT);

    }
    public int convert2Long (double a ) {
        try {
            String temp = Double.toString(a);
            return Integer.parseInt((temp.substring(0, temp.indexOf("."))));
        }
        catch (Exception e) { return 0;}
    }

}
